import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:wayofdressing/contraints.dart';
import 'package:wayofdressing/dio/UserDetailDio.dart';
import 'package:wayofdressing/dio/configureDio.dart';
import 'package:wayofdressing/all_information/user_information.dart';
import 'package:wayofdressing/dio/UserDio.dart';
import 'package:wayofdressing/good_method/show_dialog.dart';

class EditLineInformationScreen extends StatefulWidget {
  EditLineInformationScreen({required this.title});

  final String title;

  @override
  State<EditLineInformationScreen> createState() =>
      _EditLineInformationScreenState(title: title);
}

class _EditLineInformationScreenState extends State<EditLineInformationScreen> {
  _EditLineInformationScreenState({required this.title});

  final String title;
  final TextEditingController textEditingController = TextEditingController();

  Future<void> upgradeInformation() async {
    if(textEditingController.text==''){
      ShowDialog.showErrorAndDetail(context, '输入不能为空!');
      return;
    }
    FocusScope.of(context).unfocus();
    if (title == '名字' || title == '账号') {
      await updateUserById(id: userId, dio: dio,name: title=='名字'?textEditingController.text:null,account: title=='账号'?textEditingController.text:null);
      Response response=await getUserById(userId,dio);
      dynamic data=response.data['data'];
      setState(() {
        name=data['name'];
        account=data['account'];
      });
    }
    else {
      await updateUserDetailByUserId(
        dio: dio,
        userId: userId,
        bio: title == '简介' ? textEditingController.text : null,
        school: title == '学校' ? textEditingController.text : null,
        location: title == '所在地' ? textEditingController.text : null,
      );
      Response response = await getUserDetailByUserId(userId: userId, dio: dio);
      dynamic data = response.data['data'];
      setState(() {
        bio = data['bio']??'';
        location = data['location']??'未设置';
        school = data['school']??'未设置';
      });
    }
    Navigator.of(context).pop();
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    textEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        toolbarHeight: 80,
        leadingWidth: 100,
        centerTitle: true,
        title: Text(
          '修改${widget.title}',
          style: TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.bold,
            fontSize: 20,
          ),
        ),
        leading: Row(
          children: [
            const SizedBox(
              width: 10,
            ),
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Transform.scale(
                scaleX: -1,
                child: Icon(
                  Icons.play_arrow_rounded,
                  size: kAppBarButtonSize,
                  color: Colors.black.withOpacity(0.5),
                ),
              ),
            ),
          ],
        ),
        actions: [
          Row(
            children: [
              GestureDetector(
                onTap: () {
                  upgradeInformation();
                },
                child: Text(
                  '保存',
                  style: TextStyle(
                      color: Colors.redAccent,
                      fontSize: 20,
                      fontWeight: FontWeight.bold),
                ),
              ),
              SizedBox(
                width: 20,
              )
            ],
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text(
                  '我的${widget.title}',
                  style: TextStyle(color: Colors.grey, fontSize: 14),
                ),
              ],
            ),
            TextField(
              controller: textEditingController,
              decoration: InputDecoration(
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey), // 默认状态颜色
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey), // 聚焦状态颜色
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
